package com.ruoyi.erecord.device.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erecord.device.domain.bo.DeviceImportBo;
import com.ruoyi.erecord.device.domain.vo.DeviceImportDetailVo;
import com.ruoyi.erecord.device.domain.vo.DeviceImportVo;
import com.ruoyi.erecord.device.service.IDeviceImportService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 设备
 *
 * @author cc
 * @date 2023-04-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erecord/deviceImport")
public class DeviceImportController extends BaseController {

    private final IDeviceImportService iDeviceImportService;


    /**
     * 查询设备列表
     */
    @SaCheckPermission("erecord:deviceImport:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceImportVo> list(DeviceImportBo bo, PageQuery pageQuery) {
        return iDeviceImportService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设备列表
     */
    @SaCheckPermission("erecord:deviceImport:export")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceImportBo bo, HttpServletResponse response) {
        List<DeviceImportVo> list = iDeviceImportService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备", DeviceImportVo.class, response);
    }

    /**
     * 获取设备详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erecord:deviceImport:query")
    @GetMapping("/{id}")
    public R<DeviceImportVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iDeviceImportService.queryById(id));
    }

    /**
     * 新增设备
     */
    @SaCheckPermission("erecord:deviceImport:add")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceImportBo bo) {
        return toAjax(iDeviceImportService.insertByBo(bo));
    }

    /**
     * 修改设备
     */
    @SaCheckPermission("erecord:deviceImport:edit")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceImportBo bo) {
        return toAjax(iDeviceImportService.updateByBo(bo));
    }

    /**
     * 删除设备
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erecord:deviceImport:remove")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iDeviceImportService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @SaCheckPermission("erecord:deviceImport:add")
    @Log(title = "导入设备", businessType = BusinessType.IMPORT)
    @RepeatSubmit()
    @PostMapping("/importData")
    public R<Void> importData(@Validated(AddGroup.class) @RequestBody DeviceImportBo bo) {
        return toAjax(this.iDeviceImportService.importExcel(bo));
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/tpl")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "设备模板", DeviceImportDetailVo.class, response);
    }
}
